<?php
/**
 * User: 1971229701@qq.com
 * Date: 2020/9/11/011
 * Time: 9:36
 */

namespace app\admin\controller;


use think\App;
use think\Controller;
use think\facade\Session;

class BaseAdmin extends Controller
{
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $admin_info = Session::get('admin_info');

        if (!Session::has('admin_info')|| empty($admin_info)){

             $this->redirect('admin/login/do_login');

        }

        defined('UUID') || define('UUID', $admin_info['id']);
    }

    /**
     * post请求
     * @param $name
     * @param string $default
     * @param string $emptyMsg
     * @return array|mixed|null
     */
    public function _post($name, $default = '', $emptyMsg = '')
    {
        if (!$this->request->isPost()) {
            exit(apiReturnFail('请求类型错误！')->send());
        }
        if (!empty($emptyMsg)) {
            exit(apiReturnFail($emptyMsg)->send());
        }
        $request = $this->request->post($name, $default);
        return $request;
    }

    /**
     * get请求
     * @param $name
     * @param string $default
     * @param string $emptyMsg
     * @return array|mixed|null
     */
    public function _get($name, $default = '', $emptyMsg = '')
    {
        if (!$this->request->isGet()) {
            exit(apiReturnFail('请求类型错误！')->send());
        }
        if (!empty($emptyMsg)) {
            exit(apiReturnFail($emptyMsg)->send());
        }
        $request = $this->request->get($name, $default);
        return $request;
    }


    public function param($key = '', $default = '', $emptyMsg = false)
    {

        $val = input('get.' . $key, null);

        if (is_null($val)) {
            $val = input('post.' . $key, $default);
        }

        if ($emptyMsg !== false && $val == $default) {
            exit(apiReturnFail($emptyMsg)->send());
        }

        return $val;
    }
}